<?php

function list_golongan_jabatan(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');
	
	$header = array(
		'Golongan Jabatan '=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),  
		'Kriteria'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),  
		'Lembur'=>array('style'=>'text-align:left;border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:15%'), 
	);
	$facebox=true;
	$query = "SELECT * FROM golongan_jabatan ORDER BY golongan_jabatan_id ASC ";
	$result = my_query($query);
	$i=0;
	$row = array();

	while($ey = my_fetch_array($result) ){
		$i++;
		if($facebox){
			$editproperty = array(
				'href'=>'index.php?com=golongan_jabatan&task=edit&id='.$ey['golongan_jabatan_id'] ,
				'rel'=>'facebox',
				'title'=>'Edit'
			);	
		}else{
			$editproperty = array(
				'href'=>'index.php?com=golongan_jabatan&task=edit&id='.$ey['golongan_jabatan_id'] ,
				'title'=>'Edit'
			);
		}
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:;',
			'title'=>'Delete',
			'onclick'=>'confirmDelete('.$ey['golongan_jabatan_id'].')',
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );

		$row[] = array(     
			'kode'=>  $ey['kode_golongan_jabatan'] ,     
			'kelompok'=>  $ey['kelompok'] ,     
			'lembur'=>  $ey['lembur'] ,     
			'operasi'=> position_text_align( $edit_button .' '.$delete_button , 'right'),
		);
	}
	$datas = table_rows($row);
	return table_builder($header , $datas ,  5 , false );;
}

function form_golongan_jabatan_create($golongan_jabatan_id){
	$view = form_header( "golongan_jabatan" , "golongan_jabatan"  );
	$fields = my_get_data_by_id('golongan_jabatan','golongan_jabatan_id',$golongan_jabatan_id);
	 
	$kode = array(
		'name'=>'kode_golongan_jabatan',
		'type'=>'textfield',
		'id'=>'kode_golongan_jabatan',
		'value'=>isset($_POST['kode_golongan_jabatan']) ? $_POST['kode_golongan_jabatan'] : $fields['kode_golongan_jabatan'],
	);
	$form_kode_golongan_jabatan = form_dynamic($kode);
	$view .= form_field_display( $form_kode_golongan_jabatan , "Kode Golongan Jabatan" );

	$datas = array(
		'Strategis'=>'Stategis',
		'Operasional'=>'Operasional',
	);
	$kelompok = array(
		'name'=>'kelompok',
		'id'=>'kelompok',
		'value'=>isset($_POST['kelompok']) ? $_POST['kelompok'] : $fields['kelompok'],
	);
	$form_kelompok = form_dropdown($kelompok , $datas);
	$view .= form_field_display( $form_kelompok , "Kriteria" );

	if($fields['lembur']=='Y'){ 
		$lembur = array(
			'name'=>'lembur',
			'id'=>'lembur',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$lembur = array(
			'name'=>'lembur',
			'id'=>'lembur',
			'type'=>'checkbox',
		); 
	}
	$form_lembur = form_dynamic($lembur).'Lembur';
	$view .= form_field_display( $form_lembur , " " );
	
	$submit = array(
		'value' => ( $golongan_jabatan_id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	
	$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );
	$view .= form_footer( );	
	return $view;
}

function form_golongan_jabatan_submit($golongan_jabatan_id = 0){
	$lembur = isset($_POST['lembur']) ? 'Y': 'T';
	$datas = array(
			'kode_golongan_jabatan'=>my_type_data_str($_POST['kode_golongan_jabatan']), 		 	 	 	 	 	 	 
			'kelompok'	=>my_type_data_str($_POST['kelompok']), 		 	 	 	 	 	 	
			'lembur'	=>my_type_data_str($lembur), 		 	 	 	 	 	 	
		);
	if($golongan_jabatan_id > 0){
		return my_update_record('golongan_jabatan','golongan_jabatan_id',$golongan_jabatan_id, $datas);
	}
	return my_insert_record('golongan_jabatan' , $datas);
}

function form_golongan_jabatan_validate($golongan_jabatan_id){
	$errsubmit = false;
	$err = array();
	 
	$code =  trim($_POST["kode_golongan_jabatan"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Kode belum di isi";
	}

	$query="SELECT golongan_jabatan_id FROM golongan_jabatan 
		WHERE kode_golongan_jabatan='".$code.
		"' AND golongan_jabatan_id<>".$golongan_jabatan_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Kode sudah digunakan";	
	}
 
	$kelompok = trim($_POST["kelompok"]);
	if($kelompok == ''){
		$errsubmit = true;
		$err[] = "Kelompok belum diisi";
	}

	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_golongan_jabatan($golongan_jabatan_id){
	$query = "DELETE FROM golongan_jabatan WHERE golongan_jabatan_id ={$golongan_jabatan_id} ";
	return my_query($query);
}	
